package com.ruoyi.web.domain;

import lombok.Data;

@Data
public class RedPacketRequest {
    /**
     * 商户订单号
     */
    private String mchBillNo;
    /**
     * 签名
     */
    private String sign;
    /**
     * 随机字符串
     */
    private String nonceStr;
    /**
     * 商户号
     */
    private String mchId;
    /**
     * 微信AppID
     */
    private String wxAppId;
    /**
     * 红包发送者名称
     */
    private String sendName;
    /**
     * 用户openid
     */
    private String reOpenId;
    /**
     * 金额 单位 分
     */
    private int totalAmount;
    private int totalNum = 1;
    /**
     * 祝福语
     */
    private String wishing;
    /**
     *
     */
    private String clientIp;
    /**
     * 活动名称
     */
    private String actName;
    /**
     * 备注信息
     */
    private String remark;
    /**
     * API密钥
     */
    private String apiKey;

    private String sceneId;

    public RedPacketRequest(){}

    public RedPacketRequest(String mchBillNo, String mchId, String wxAppId, String sendName, 
                            String reOpenId, int totalAmount, String wishing, 
                            String clientIp, String actName, String remark, String apiKey,String sceneId) {
        this.mchBillNo = mchBillNo;
        this.mchId = mchId;
        this.wxAppId = wxAppId;
        this.sendName = sendName;
        this.reOpenId = reOpenId;
        this.totalAmount = totalAmount;
        this.wishing = wishing;
        this.clientIp = clientIp;
        this.actName = actName;
        this.remark = remark;
        this.apiKey = apiKey;
        this.sceneId = sceneId;
    }

   /* public String toXml() {
        Map<String, String> params = new HashMap<>();
        params.put("mch_billno", mchBillNo);
        params.put("mch_id", mchId);
        params.put("wxappid", wxAppId);
        params.put("send_name", sendName);
        params.put("re_openid", reOpenId);
        params.put("total_amount", String.valueOf(totalAmount));
        params.put("total_num", String.valueOf(totalNum));
        params.put("wishing", wishing);
        params.put("client_ip", clientIp);
        params.put("act_name", actName);
        params.put("remark", remark);
        params.put("nonce_str", generateNonceStr());

        String sign = generateSign(params);
        params.put("sign", sign);

        StringBuilder xml = new StringBuilder("<xml>");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            xml.append("<").append(entry.getKey()).append(">")
               .append(entry.getValue())
               .append("</").append(entry.getKey()).append(">");
        }
        xml.append("</xml>");
        return xml.toString();
    }

    private String generateNonceStr() {
        return Long.toHexString(System.currentTimeMillis());
    }

    public String generateSign(Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        params.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .forEach(entry -> sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&"));
        sb.append("key=").append(apiKey);
        return DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }*/
}
